import Paging from '../../../../wxjs/Paging.js'
var pag = new Paging();
var app = getApp()

Page(Object.assign(pag.func(),{
  data: {
    currentPage: 'mine',
    aliveCount: 0,
    form: {}
  },
  listParams: {},
  reqUrl:app.serAddrs['getMessageList'],
  header: {'content-type': 'application/x-www-form-urlencoded'},
  onLoad(){
    this.pageListInit();

    this.getLiveCount()
  },
  // 查看存活消息
  getLiveCount(){
    var _this = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['getUnreadMessage'],
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        _this.setData({
          aliveCount: res.data.aliveCount
        })
      }
    })
  },
  // 拒绝
  reject(e){
    var appyStatus = -1 // 同意
    this.updateInvitation(e,appyStatus)
  },
  //同意
  arrow(e){
    var appyStatus = 1 // 同意
    this.updateInvitation(e,appyStatus)
  },
  updateInvitation(e,appyStatus){
    var {id,childUserId,appyUserId,appyRelation,relationType} = e.detail
    if (appyRelation===null){
      appyRelation = ''
    }
    var form = {
      id,childUserId,appyUserId,relationType,appyRelation,
      appyStatus
    }
    var that = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['updateInvitationStatus'],
      data: form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        var title = appyStatus == -1?'拒绝成功':'同意成功'
        wx.showToast({
          title,
          success(){
            var lists = that.data.lists
            lists.appyStatus = appyStatus
            lists.some(item => {
              if (item.id == id) {
                item.appyStatus = appyStatus
              }
            })
            that.setData({
              lists
            })
            that.updatePrevPageData()
          }
        })
      },
      fail(err){
        // var title = appyStatus == -1?'拒绝添加失败':'同意添加失败'
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }
    })
  },
  // 更新上一页面的数据
  updatePrevPageData(){
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2]
    var aliveCount = this.data.aliveCount - 1
    if (aliveCount==0){
      aliveCount = ''
    }
    this.setData({
      aliveCount
    })
    prevPage.setData({
      aliveCount
    })
  }
}))
